//导入request.js请求工具
import request from '@/utils/request.js'

//管理员登录
export const adminLoginService = (loginData) => {
    return request.post('/admin/login', loginData)
}

//分页获取图书
export const getBookPage = (data) => {
    return request.post('/admin/getBookPage', data)
}

//获取图书分类
export const getCategories = () => {
    return request.get('/admin/getCategories')
}

//修改图书
export const upBook = (data) => {
    return request.post('/admin/upBook', data)
}

//图书删除
export const deleteBook = (data) => {
    const params = new URLSearchParams()
    for (let key in data) {
        params.append(key, data[key])
    }
    return request.delete('/admin/deleteBook', {params})
}

//新增图书
export const saveBook = (data) => {
    return request.post('/admin/saveBook', data)
}

//获取分类信息
export const getCategoryPage = (data) => {
    return request.post('/admin/getCategory', data)
}

//修改分类信息
export const upCategory = (data) => {
    return request.post('/admin/upCategory', data)
}

//新增分类
export const saveCategory = (data) => {
    return request.post('/admin/saveCategory', data)
}

//删除分类
export const deleteCategory = (data) => {
    const params = new URLSearchParams()
    for (let key in data) {
        params.append(key, data[key])
    }
    return request.delete('/admin/deleteCategory', {params})
}

//获取图书借阅申请表
export const getBorroweuests = (data) => {
    return request.post('/admin/getBorroweuests', data)
}

//获取用户借阅历史分页列表
export const getUserBorrow = (data) => {
    return request.post('/admin/getUserBorrow', data)
}

//申请图书状态修改
export const upBorroweuestsStatus = (data) => {
    return request.post('/admin/upBorroweuestsStatus', data)
}

//发送逾期邮件
export function sendOverdueEmail(borrowHistoryId) {
    return request({
        url: '/admin/sendOverdueEmail',
        method: 'post',
        params: { borrowHistoryId }
    })
}

// 获取用户列表
export function getUserList(data) {
    return request({
        url: '/admin/users',
        method: 'post',
        data
    })
}

// 更新用户信息
export function updateUser(data) {
    return request({
        url: `/admin/users/${data.id}`,
        method: 'put',
        data
    })
}

// 删除用户
export function deleteUser(id) {
    return request({
        url: `/admin/users/${id}`,
        method: 'delete'
    })
}

// 封禁用户
export function banUser(data) {
    return request({
        url: '/admin/users/ban',
        method: 'post',
        data
    })
}

// 解除封禁
export function unbanUser(data) {
    return request({
        url: '/admin/users/unban',
        method: 'post',
        data
    })
}

// 获取封禁验证码
export function getBanVerificationCode(userId) {
    return request({
        url: `/admin/verify/user/ban/${userId}`,
        method: 'get'
    })
}

// 获取解封验证码
export function getUnbanVerificationCode(userId) {
    return request({
        url: `/admin/verify/user/unban/${userId}`,
        method: 'get'
    })
}

// 更新用户状态（封禁/解封）
export const updateUserStatus = (userId, banDTO, verifyDTO) => {
    // 合并状态信息和验证码
    const statusUpdateDTO = {
        status: banDTO.status,
        banReason: banDTO.banReason,
        banDuration: banDTO.banDuration,
        verificationCode: verifyDTO.verificationCode
    }

    return request.put(`/admin/user/status/${userId}`, statusUpdateDTO)
}

// 获取管理员列表
export const getAdminList = (data) => {
    return request.post('/admin/adminUsers', data)
}

// 获取角色列表
export const getRoleList = () => {
    return request.get('/admin/roles')
}

// 修改管理员信息
export const updateAdmin = (data) => {
    return request.put('/admin/adminUser', data)
}

// 修改管理员状态（启用/禁用）
export const updateAdminStatus = (data) => {
    return request.put('/admin/adminUser/status', data)
}

// 新增管理员
export const createAdmin = (data) => {
    return request.post('/admin/createAdminUser', data)
}

// 获取删除管理员验证码
export const getAdminDeleteVerifyCode = (adminId) => {
    return request.get(`/admin/verify/admin/delete/${adminId}`)
}

// 删除管理员
export const deleteAdmin = (id, verificationCode) => {
    return request.delete(`/admin/adminUser/${id}`, {
        data: {
            id: id,
            verificationCode: verificationCode
        }
    })
}

// 获取角色列表（分页）
export const getRoleListPage = (data) => {
    return request.post('/admin/roles/page', data)
}

// 获取所有权限列表
export const getPermissionList = () => {
    return request.get('/admin/permissions')
}

// 新增角色
export const addRole = (data) => {
    return request.post('/admin/saveRoles', data)
}

// 更新角色
export const updateRole = (data) => {
    return request.put(`/admin/roles/${data.id}`, {
        id: data.id,
        name: data.name,
        description: data.description,
        permissions: data.permissions
    })
}

// 获取删除角色验证码
export const getRoleDeleteVerifyCode = (roleId) => {
    return request.get(`/admin/verify/role/delete/${roleId}`)
}

// 删除角色
export const deleteRole = (roleId, code) => {
    return request.delete(`/admin/roles/${roleId}`, {
        data: {
            roleId: roleId,
            code: code
        }
    })
}

// 获取管理员个人资料
export function getAdminProfile() {
    return request({
        url: '/admin/profile',
        method: 'get'
    })
}

// 更新管理员个人资料
export function updateAdminProfile(data) {
    return request({
        url: '/admin/profile',
        method: 'put',
        data
    })
}

// 修改密码
export function changePassword(data) {
    return request({
        url: '/admin/password',
        method: 'post',
        data
    })
}

// 归还图书
export function returnBook(borrowHistoryId) {
    return request({
        url: '/admin/return-book',
        method: 'post',
        params: { borrowHistoryId }
    })
}
